package com.demo.service.impl;


import java.util.Date;
import java.util.List;

import com.demo.entity.constants.Constants;
import com.demo.entity.enums.PostStatusEnum;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.Comment;
import com.demo.entity.po.PostInfo;

import com.demo.entity.query.CommentQuery;
import com.demo.entity.query.PostInfoQuery;

import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.CommentMapper;
import com.demo.service.PostInfoService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.PostInfoMapper;

import com.demo.entity.enums.PageSize;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:帖子表serviceImpl
 * @Date:2025-03-01
 * @author：author
*/
@Service("postInfoService")
public class PostInfoServiceImpl implements PostInfoService{

	@Resource
	private PostInfoMapper<PostInfo,PostInfoQuery> postInfoMapper;

	@Resource
	private CommentMapper<Comment,CommentQuery> commentMapper;


	/**
	 * 根据条件查询列表
	 */
	public List<PostInfo> findListByParam(PostInfoQuery query) { 
		return this.postInfoMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(PostInfoQuery query){
		return this.postInfoMapper.selectCount(query);
	}




	/**
	 * 分页查询
	 */
	public PaginationResultVO<PostInfo> findListByPage(PostInfoQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<PostInfo> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(PostInfo bean) { 
		return this.postInfoMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<PostInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.postInfoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<PostInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.postInfoMapper.insertOrUpdateBatch(listBean);
	}

	@Override
	public PostInfo getPostInfoByPostId(Long postId) {
		return null;
	}



	/**
	 * 根据PostId更新
	 */
	public Integer updatePostInfoByPostId(PostInfo bean, Long postId) { 
		return this.postInfoMapper.updateByPostId(bean,postId);
	}

	/**
	 * 根据PostId删除
	 */
	public Integer deletePostInfoByPostId(Long postId) { 
		return this.postInfoMapper.deleteByPostId(postId);
	}


	/**
	 * 保存帖子信息
	 * @param posts
	 */
	@Override
	public void savePost(PostInfo posts) {

		PostInfo postsDB=null;
		if (posts.getPostId()!=null){
			postsDB=this.postInfoMapper.selectByPostId(posts.getPostId());
		}
		if (postsDB!=null&&!postsDB.getUserId().equals(posts.getUserId())){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}

		Date curDate = new Date();
		if (posts.getPostId() == null) {
			posts.setCreateTime(curDate);
			posts.setUpdateTime(curDate);
			posts.setIsDraft(Constants.ZERO);
			// 新增
			posts.setStatus(PostStatusEnum.DRAFT.getStatus());
			postInfoMapper.insert(posts);
		}else {
			posts.setUpdateTime(curDate);
			postInfoMapper.updateByPostId(posts,posts.getPostId());
		}
	}


	/**
	 * 根据Id 查询信息
	 */
	@Transactional(rollbackFor = BusinessException.class)
	public PostInfo getPostsById(Long id) {
		CommentQuery commentsQuery = new CommentQuery();
		commentsQuery.setPostId(id);
		Integer count = commentMapper.selectCount(commentsQuery);
		PostInfo posts = this.postInfoMapper.selectByPostId(id);
		posts.setCommentCount(count);
		posts.setViewCount(posts.getViewCount()+ Constants.ONE);
		postInfoMapper.updateByPostId(posts,posts.getPostId());
		return posts;
	}

	/**
	 *
	 * @param loginUserId 当前登d录用户的i
	 * @param postId 帖子Id
	 * @param status 状态id
	 */
	@Override
	public void updateStatus(Long loginUserId, Long postId, Integer status) {
		PostStatusEnum statusEnum = PostStatusEnum.getByStatus(status);
		if (statusEnum == null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		PostInfo posts = postInfoMapper.selectByPostId(postId);
		if (posts == null||!posts.getUserId().equals(loginUserId)) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		posts.setStatus(status);
		postInfoMapper.updateByPostId(posts, postId);
	}


	/**
	 *
	 * @param userId 当前登录用户id
	 * @param id 帖子id
	 */
	@Override
	public void deletePosts(Long userId, Long id) {
		PostInfo posts = postInfoMapper.selectByPostId(id);
		if (posts == null||!posts.getUserId().equals(userId)) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		postInfoMapper.deleteByPostId(id);
	}



}